package org.mzy.order.service.impl;

import org.mzy.basic.config.BussinessException;
import org.mzy.basic.service.impl.BaseServiceImpl;
import org.mzy.order.domain.OrderProduct;
import org.mzy.order.mapper.OrderProductMapper;
import org.mzy.order.service.IOrderProductService;
import org.mzy.serve.domain.Product;
import org.mzy.serve.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzy
 * @since 2022-11-03
 */
@Service
public class OrderProductServiceImpl extends BaseServiceImpl<OrderProduct> implements IOrderProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OrderProductMapper orderProductMapper;

    @Override
    public void generate(Long id) {
        // 根据id获取到商品信息
        Product product = productMapper.findById(id);
        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setState(0);
        orderProduct.setProductId(product.getId());
        orderProductMapper.add(orderProduct);
    }

    // 确认订单
    @Override
    public void confirm(Long orderId) {
        // 通过id获取商品信息
        Product product = productMapper.findById(orderId);
//        if (product == null) {
//            throw new BussinessException("商品不存在");
//        }
//        OrderProduct orderProduct = orderProductMapper.findByProductId(orderId);
//        if (orderProduct == null) {
//            throw new BussinessException("订单不存在");
//        }
        // 交易完成
//        orderProduct.setState(1);
//        orderProduct.setLastConfirmTime(new Date());
//        orderProductMapper.update(orderProduct);
        // 商品下架
        product.setState(0);
        productMapper.update(product);
    }

    // 取消订单
    @Override
    public void cancel(Long orderId) {
        // 通过id获取商品信息
        Product product = productMapper.findById(orderId);
//        if (product == null) {
//            throw new BussinessException("商品不存在");
//        }
//        OrderProduct orderProduct = orderProductMapper.findByProductId(orderId);
//        if (orderProduct == null) {
//            throw new BussinessException("订单不存在");
//        }
        // 修改状态
//        orderProduct.setState(-1);
        product.setState(1);
        productMapper.update(product);
//        orderProductMapper.update(orderProduct);
    }
}
